/*
 * Speciation.h
 *
 *  Created on: 13 Jun 2011
 *      Author: allan
 */

#ifndef SPECIATION_H_
#define SPECIATION_H_

// C++ includes
#include <functional>
#include <vector>
using namespace std;
using namespace std::placeholders;

// GeoReact includes
#include "System.h"

class Speciation 
{
public:
	Speciation();
	
	virtual ~Speciation();
	
	void SetSystem(System& system);
	
	void ImposeChargeBalance();
	
	void ImposeUj(unsigned iPrimary, double value);
	
	void ImposeCj(unsigned iPrimary, double value);
	
	void ImposeCe(unsigned iEquilibrium, double value);
	
	void ImposeHj(unsigned iPrimary, double value);
	
	bool Solve(double tolerance, unsigned maxiterations);
	
private:
	void NonLinearSystemFunction(const vector<double>& S, vector<double>& F);
	
	void NonLinearSystemJacobian(const vector<double>& S, vector<vector<double>>& J);
	
	void NonLinearSystemFunctionJacobian(const vector<double>& S, vector<double>& F, vector<vector<double>>& J);
	
	void UpdateComposition(const vector<double>& S);
	
private:
	System* system;
	
	vector<double> zj, ze; // the electrical charges of the primary and equilibrium species
	
	vector<double> Cj;
	
	vector<double> Ce;
	
	vector<double> aj;
	
	vector<double> ae;
	
	vector<double> Qe;
	
	vector<double> Ke;
	
	vector<double> h;
	
	vector<vector<double>> alpha;
	
	vector<vector<double>> beta;
	
	vector<vector<double>> theta;
	
	vector<vector<double>> mu;
	
	unsigned currentCondition;
};

#endif /* SPECIATION_H_ */
